package com.onccc.social.step

import android.text.TextUtils
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ScreenUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.paste
import com.onccc.social.AssistsWindowManager
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getComment
import com.onccc.social.MainActivity.Companion.getInterest
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CommentModel
import com.onccc.social.model.ImageInterestModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay
import kotlin.random.Random

/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class DouyinSocial : StepImpl() {

    //    var mPackageName = "com.ss.android.ugc.aweme.lite"
    var mPackageName = "com.ss.android.ugc.aweme"

    private var maxSelect = 2
    private var selectIndex = 0
    private var mCommentModel = CommentModel()
    private var mImageInterestModel = ImageInterestModel()
    private var videoIndex = 0

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动抖音")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getOneTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            Assists.findByText("关闭").forEach() {
                OverManager.log("关闭未成人提示")
                it.click()
                runIO { delay(1000) }
            }
            getInterest({ interestModel ->
                mImageInterestModel = interestModel
                StepManager.execute(DouyinSocial::class.java, StepTag.STEP_4, begin = true)
            })
            {
                StepManager.execute(DouyinSocial::class.java, StepTag.STEP_3, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_3) {
            runIO { delay(getTwoTime()) }

            selectIndex++
            if (selectIndex > maxSelect) {
                return@next Step.get(StepTag.STEP_100)
            }

            OverManager.log("向下滑动")
            AssistsWindowManager.switchNotTouchableAll()
            runIO { delay(250) }
            val x = ScreenUtils.getAppScreenWidth() / 2F
            val distance = ScreenUtils.getAppScreenHeight() / 2F
            val startY = distance + distance / 2F
            val endY = distance - distance / 2F
            OverManager.log("滑动：$x/$startY,$x/$endY")
            Assists.gesture(
                    floatArrayOf(x, startY), floatArrayOf(x, endY), 0, 1500L
            )
            AssistsWindowManager.switchTouchableAll()

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_4) {
            var time = 0
            if (mImageInterestModel.isToLike) {
                time = 3000 + Random.nextInt(5000)
            }
            if (mImageInterestModel.isToFollow) {
                time += Random.nextInt(5000)
            }
            if (mImageInterestModel.isToComment) {
                time += Random.nextInt(5000)
            }
            runIO { delay(time.toLong()) }

            if (mImageInterestModel.isToLike) {
                runIO { delay(1500) }
                Assists.findByText("未点赞").forEach {
                    val screen = it.getBoundsInScreen()
                    OverManager.log("关注")
                    it.click()
                }
                return@next Step.get(StepTag.STEP_11)
            }
            return@next Step.get(StepTag.STEP_11)
        }.next(StepTag.STEP_11) {
            if (mImageInterestModel.isToFollow) {
                runIO { delay(1500) }
                Assists.findByTags("android.widget.Button").forEach {
                    Assists.findById("com.ss.android.ugc.aweme:id/ix0").forEach {
                        val screen = it.getBoundsInScreen()
                        if (screen.top < 2000 && screen.bottom > 0) {
                            OverManager.log("关注")
                            it.click()
                        }
                    }
                }
                return@next Step.get(StepTag.STEP_12)
            }
            return@next Step.get(StepTag.STEP_12)
        }.next(StepTag.STEP_12) {
            if (mImageInterestModel.isToComment) {
                return@next Step.get(StepTag.STEP_5)
            }
            return@next Step.get(StepTag.STEP_3)
        }.next(StepTag.STEP_5) {
            runIO { delay(1000) }
            OverManager.log("查找评论按钮")

            Assists.findByText("评论").forEach() {
                val screen = it.getBoundsInScreen()
                if (screen.top < 2000 && screen.bottom > 0) {
                    OverManager.log("点击评论按钮")
                    it.click()
                    runIO { delay(1000) }
                    return@next Step.get(StepTag.STEP_6)
                }
            }

            return@next Step.none
        }.next(StepTag.STEP_6) {
            runIO { delay(1000) }
            OverManager.log("获取评论")
            val stringBuffer = StringBuilder()
            Assists.getAllNodes().forEach {
                if (it.text != null && !TextUtils.isEmpty(it.text.toString()) && !it.text.isNullOrEmpty()) {
                    stringBuffer.append(it.text.toString())
                }
            }
            getComment({ commentModel ->
                mCommentModel = commentModel
                StepManager.execute(DouyinSocial::class.java, StepTag.STEP_7, begin = true)
            })
            {
                StepManager.execute(DouyinSocial::class.java, StepTag.STEP_9, begin = true)
            }
            return@next Step.none
        }.next(StepTag.STEP_7) {
            runIO { delay(1000) }
            OverManager.log("发送评论")
            Assists.findByTags("android.widget.EditText").forEach {
                it.click()
                runIO { delay(1500) }
                Assists.findByTags("android.widget.EditText").forEach { it1 ->
                    val screen = it1.getBoundsInScreen()
                    if (screen.top < 1600 && screen.bottom < 1600) {
                        it1.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                        runIO { delay(MainActivity.getOneTime()) }
                        for (s in mCommentModel.comment) {
                            it.paste(s.toString())
                            runIO { delay(MainActivity.getInputTime()) }
                        }

                        runIO { delay(1500) }
                        return@next Step.get(StepTag.STEP_8)
                    }

                }
            }
            return@next Step.none
        }.next(StepTag.STEP_8) {
            OverManager.log("点击发送")
            Assists.findById("com.ss.android.ugc.aweme:id/dyu").forEach {
                val screen = it.getBoundsInScreen()
                if (screen.top < 1600 && screen.bottom < 1600) {
                    it.parent.click()
                    runIO { delay(1000) }
                    return@next Step.get(StepTag.STEP_9)
                }
            }
            return@next Step.none
        }.next(StepTag.STEP_9) {
            runIO { delay(1000) }
            OverManager.log("关闭评论区")
            Assists.findById("com.ss.android.ugc.aweme:id/back_btn").firstOrNull {
                it.click()
                runIO { delay(1000) }
                return@next Step.get(StepTag.STEP_3)
            }
            return@next Step.none
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }


}